layui.use(['layer', 'table'], function () {
    let table = layui.table,
        layer = layui.layer;
    layui.layer.load(0, {shade: [0.5, '#fff']});
    /*查询所有公告的信息*/
    table.render({
        elem: '#grade-table'
        , height: 420
        , toolbar: true
        , url: '/getAllFinalGrade'
        , where: {queryKey: ''}
        , cols: [
            [ //标题栏
                {
                    field: 'paper', title: '论文编号', width: 90, align: 'center', fixed: 'left', templet: function (res) {
                        return res.paper.bh;
                    }
                },
                {
                    field: 'paper', title: '论文标题', align: 'center', fixed: 'left', templet: function (res) {
                        return res.paper.title;
                    }
                },
                {field: 'sno', title: '学号', width: 140, align: 'center', fixed: 'left'},
                {
                    field: 'paper', title: '姓名', width: 100, align: 'center', templet: function (res) {
                        return res.paper.sname;
                    }
                },
                {
                    field: 'paper', title: '所属学院', width: 130, align: 'center', templet: function (res) {
                        return res.paper.title1.collegeName;
                    }
                },
                {
                    field: 'paper', title: '所属专业', width: 140, align: 'center', templet: function (res) {
                        return res.paper.title1.majorName;
                    }
                },
                {
                    field: 'paper', title: '指导老师', width: 110, align: 'center', templet: function (res) {
                        return res.paper.title1.TName;
                    }
                },

                {
                    field: 'paper', title: '老师工号', width: 80, align: 'center', templet: function (res) {
                        return res.paper.title1.tno;
                    }
                },
                {
                    field: 'finalGrade', title: '分数', width: 130, align: 'center', templet: function (res) {
                        if (res.finalGrade === null || res.finalGrade === '') {
                            return '- -';
                        } else {
                            let grade = res.finalGrade;
                            if (grade < 60) {
                                return '<span style="color: red">' + grade + '</span>'
                            } else if (grade > 80) {
                                return '<span style="color: #009688">' + grade + '</span>'
                            } else {
                                return grade;
                            }
                        }
                    }
                },
                {
                    field: 'groupGrade', title: '答辩次数', width: 80, align: 'center', templet: function (res) {
                        return res.groupGrade.reply;
                    }
                },
                {
                    field: 'paper', title: '论文状态', width: 100, align: 'center', templet: function (res) {
                        return res.paper.state;

                    }
                },
                {
                    field: 'passState', title: '审核状态', width: 100, align: 'center', templet: function (res) {
                        if (res.passState === null || res.passState === '') {
                            return '<span style="color: red">审核中</span>'
                        } else if (res.state === '不通过') {
                            return '<span style="color: red">不通过</span>'
                        } else if (res.state === '通过') {
                            return '<span style="color: #009688">通过</span>'
                        }

                    }
                },
                {title: '课题详情', align: 'center', width: 120, toolbar: '#task-table-tool'},
                {title: '评分操作', align: 'center', width: 150, toolbar: '#assignment-table-tool', fixed: 'right'},
                {field: 'adviserGrade', title: '指导老师', width: 120, align: 'center', hide: true},
                {field: 'grading', title: '评阅老师', width: 100, align: 'center', hide: true},
                {field: 'groupGrade', title: '答辩小组成绩', width: 140, align: 'center', hide: true},
                {field: 'paper', title: '论文', width: 240, align: 'center', hide: true},
            ]
        ]
        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [6, 12, 18]
        , limit: 6
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 50px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    //监听表头事件  监听对象  allUSer
    table.on('toolbar(grade-table)', function (obj) {

        //查询事件
        if (obj.event === 'query') {  //按关键字搜索事件
            let queryKey = $('#queryKey').val().trim(); //获取搜索的关键字
            if (queryKey === null || queryKey === '') {
                layer.msg('请输入要查询内容', {offset: '250px', time: 1500, anim: 6, area: ['200px', '50px']});
                return false;
            }
            table.reload('grade-table', {
                where: {queryKey: queryKey} //设定异步数据接口的额外参数
                //,url: '/queryAll.indexDo' //重载接口地址 如果接口地址与原地址一样则不用写此项参数
            });

        } else if (obj.event === 'add') {  //查询全部
            table.reload('grade-table', {
                where: {queryKey: ''} //设定异步数据接口的额外参数
                //,url: '/queryAll.indexDo' //重载接口地址 如果接口地址与原地址一样则不用写此项参数
            });
        }
    });


    /*监听操作栏事件*/
    table.on('tool(grade-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'taskDetail') {  //编辑事件
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '600px']
                , content: $("#task-detail")
                , title: '学生课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTitle(data);
                }
                , end: function () {
                    $("#task-detail").css("display", 'none');
                }
            });
        } else if (obj.event === 'export-grade') {  //导出word
            setStuInfo(data);
            setGrade(data);
            let fileName = data.sno + data.sname + '的评分明细';
            $("#teachers-grade").wordExport(fileName);

        } else if (obj.event === 'check-grade') {  //审核
            if (data.paper.state !== '最终审核') {
                layer.msg('此论文不处于最终审核阶段,您可以先点击<i class="layui-icon layui-icon-search"/>按钮查看详情');
                return false;
            }
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['1040px', '800px']
                , content: $("#teacher-grade-box")
                , title: '中期检查'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    setStuInfo(data);
                    setGrade(data);
                }
                , end: function () {
                    $("#teacher-grade-box").css("display", 'none');
                }
            });
        } else if (obj.event === 'query-grade') {
            layer.open({
                type: 1
                , offset: 0
                , skin: 'layui-layer-rim'
                , area: ['1040px', '800px']
                , content: $("#teacher-query-box")
                , title: '中期检查'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    setStuInfo(data);
                    setGrade(data);
                    $("#grade-detail").html($("#teachers-grade").html());
                }
                , end: function () {
                    $("#teacher-query-box").css("display", 'none');
                }
            });
        }
    });


    //通过
    $("#check-pass").click(function () {
        let result = '通过';
        checkResult(result);
    });

    //不通过审核
    $("#check-no-pass").click(function () {
        let result = '不通过';
        checkResult(result);
    });

    layui.layer.closeAll('loading');

});


//设置标题信息
function setStuInfo(data) {
    $("#sNO").html(data.paper.sno);
    $("#sName").html(data.paper.sname);
    $("#major").html(data.paper.title1.majorName);
    $("#title").html(data.paper.title)
    $("#table-sName").html(data.paper.sname);
}


//提交获取审核结果
function checkResult(result) {
    layui.layer.closeAll('page');
    let opinion = $("#check-opinion").val().trim();
    layui.layer.msg(result + "-----------" + opinion);
}


//设置查询到的各项成绩的信息
function setGrade(data) {
    //指导老师
    if (data.adviserGrade !== null && data.adviserGrade !== '') {
        let adsGrade1 = data.adviserGrade.grade1;
        let adsGrade2 = data.adviserGrade.grade2;
        let adsGrade3 = data.adviserGrade.grade3;
        let adsGrade4 = data.adviserGrade.grade4;
        let adsGrade5 = data.adviserGrade.grade5;
        let adsGrade6 = data.adviserGrade.grade6;
        let adsGrade = adsGrade1 + adsGrade2 + adsGrade3 + adsGrade4 + adsGrade5 + adsGrade6;
        $("#teacher-grade").html(adsGrade);
        $("#teacher-grade1").html(adsGrade1);
        $("#teacher-grade2").html(adsGrade2);
        $("#teacher-grade3").html(adsGrade3);
        $("#teacher-grade4").html(adsGrade4);
        $("#teacher-grade5").html(adsGrade5);
        $("#teacher-grade6").html(adsGrade6);
        $("#teacher-remark").html(data.adviserGrade.remark);
    }

    //评阅老师
    if (data.grading !== null && data.grading !== '') {
        let gradingGrade1 = data.grading.grade1;
        let gradingGrade2 = data.grading.grade2;
        let gradingGrade3 = data.grading.grade3;
        let gradingGrade = gradingGrade3 + gradingGrade2 + gradingGrade1;
        $("#grading-grade").html(gradingGrade);
        $("#grading-grade1").html(gradingGrade1);
        $("#grading-grade2").html(gradingGrade2);
        $("#grading-grade3").html(gradingGrade3);
        $("#grading-remark").html(data.grading.remark);
    }

    //答辩小组
    if (data.groupGrade !== null && data.groupGrade !== '') {
        let groupGrade1 = data.groupGrade.grade1;
        let groupGrade2 = data.groupGrade.grade2;
        let groupGrade3 = data.groupGrade.grade3;
        let groupGrade4 = data.groupGrade.grade4;
        let groupGrade = groupGrade1 + groupGrade2 + groupGrade3 + groupGrade4;
        $("#group-grade").html(groupGrade);
        $("#group-grade1").html(groupGrade1);
        $("#group-grade2").html(groupGrade2);
        $("#group-grade3").html(groupGrade3);
        $("#group-grade4").html(groupGrade4);
        $("#group-remark").html(data.groupGrade.remark);
    }

}



